package com.keyin.android.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.util.Log;

public class Tools {
	
	private static final String TAG = "UMessage";

	public static boolean isEmptyString(String s) {
		return s == null || s.equals("");
	}


	public static void printException(Exception e){
		String message = e.getMessage();
		Log.e(TAG, message == null ? "null" : message);
		StackTraceElement[] elements = e.getStackTrace();
		for (int i = 0; i < elements.length; i ++){
			Log.e(TAG, elements[i].toString());
		}
	}
	
	public static String md5encrypt(String strIn) throws NoSuchAlgorithmException {
		MessageDigest md = MessageDigest.getInstance("MD5");
		md.update(strIn.getBytes());
		byte b[] = md.digest();
		StringBuffer buf = new StringBuffer("");
		for (int i = 0; i < b.length; ++i) {
			int x = b[i] & 0xFF, h = x >>> 4, l = x & 0x0F;
			buf.append((char) (h + ((h < 10) ? '0' : 'a' - 10)));
			buf.append((char) (l + ((l < 10) ? '0' : 'a' - 10)));
		}
		return buf.toString();
	}
    
    public static void printByteArray(String tag, byte[] ab) {
    	Log.i("Umessage", tag);
		char c[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
				'B', 'C', 'D', 'E', 'F' };
		for (int i = 0; i < ab.length; i++) {
			if (i == 0) {
			} else if (i % 16 == 0) {
				System.out.println("");
			} else if (i % 4 == 0) {
				System.out.print(" ");
			}
			int nVal = ab[i];
			if (nVal < 0) {
				nVal = 128 + nVal + 128;
			}
			String s = "";
			int n = nVal / 16;
			s += c[n];
			n = nVal % 16;
			s += c[n];
			System.out.print(s + " ");
		}
		
		System.out.println("\n-------------------------------------------\n");
	}
}
